﻿using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Net;
using com.stockstar.baseinfo.datacontract;
using RemoteBUInterface;
using System.Text.RegularExpressions;
using com.stockstar.baseinfo.datacontract.param;
using System.Web.Script.Serialization;


/// <summary>
/// Summary description for StringOper
/// </summary>
public class StringOper
{
    IRemoteQuoteBU quoteOper = (IRemoteQuoteBU)Activator.GetObject(typeof(IRemoteQuoteBU), "tcp://192.168.17.83:8801/RemoteBUHostSvr/RemoteQuoteBU");

	public StringOper()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    /// <summary>
    /// 表格颜色交替
    /// </summary>
    /// <param name="col">行号</param>
    /// <param name="style1">样式1</param>
    /// <param name="style2">样式2</param>
    /// <returns></returns>
    public string changeStyle(int col, string style1, string style2) 
    {
        if (col % 2 == 1)
        {
            return style1;
        }
        else 
        {
            return style2;
        }
    }

    /// <summary>
    /// 数字比较套红
    /// </summary>
    /// <param name="val"></param>
    /// <param name="tag"></param>
    /// <param name="formatStr"></param>
    /// <returns></returns>   
    public string getUpdownStyle(double val, double tag,string formatStr) 
    {
        if ((val - tag) > 0) 
        {
            return "<span class=\"font_color_red\">" + val.ToString(formatStr) + "</span>";
        }
        else if ((val - tag) < 0)
        {
            return "<span class=\"font_color_green\">" + val.ToString(formatStr) + "</span>";
        }
        else 
        {
            return val.ToString(formatStr);
        }
    }

    /// <summary>
    /// 验证基金代码
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public bool chkFundCode(string code)
    {
        #region 走BaseFundCom,老的基金验证 2012-07-13下线处理
        //if (PubBean.AllFund == null || PubBean.AllFund.Count <= 0) 
        //{
        //    PubBean.getAllFund();
        //}
        //if (PubBean.AllFund.ContainsKey(code))
        //{
        //    return true;
        //}
        //return false;

        #endregion

        #region 新的验证基金代码，从运河接口取到基金代码表
        return FundCommon.FundCodesListInstance.Contains(code);
        #endregion
    }

    public bool chkFundCodeExt(string code)
    {
        var t = PubBean.getAllFundExt();
        //HttpContext.Current.Response.Write(t == null);
        return t != null && t.Count > 0 && t.ContainsKey(code);
    }

    /// <summary>
    /// 验证封闭式基金
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public bool isFundFb(string code) 
    {
        FundCode fund = getFundCode(code);
        if (fund != null) 
        {
            if (fund.SecurityType == 1 || fund.SecurityType == 4 || fund.SecurityType == 5 || fund.SecurityType == 7) 
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 验证货币式基金
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public bool isFundHb(string code) 
    {
        FundCode fund = getFundCode(code);
        if (fund != null)
        {
            if (fund.InvestType == 3 )
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 字符截取
    /// </summary>
    /// <param name="str"></param>
    /// <param name="length"></param>
    /// <returns></returns>
    public string subStr(string str, int length) 
    {
        if (str.Length > length)
        {
            return str.Substring(0, length);
        }
        else 
        {
            return str;
        }
    }

    /// <summary>
    /// 获取基金基本信息
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public FundCode getFundCode(string code) 
    {
        if (PubBean.AllFund == null || PubBean.AllFund.Count <= 0)
        {
            PubBean.getAllFund();
        }
        if (PubBean.AllFund.ContainsKey(code))
        {
            return PubBean.AllFund[code];
        }
        return null;
    }

    public FundCode getFundCodeExt(string code)
    {
        var t = PubBean.getAllFundExt();
        if (t != null && t.Count > 0 && t.ContainsKey(code))
        {
            return t[code];
        }
        return null;
    }

    /// <summary>
    /// 获取基金类型
    /// </summary>
    /// <param name="typeid"></param>
    /// <returns></returns>
    public string getFundType(int typeid) 
    {
        switch (typeid) 
        {
            case 1:
                return "契约型封闭式";
            case 2:
                return "契约型开放式";
            case 3:
                return "其它";
            case 4:
                return "ETF";
            case 5:
                return "LOF";
            case 6:
                return "外币基金";
            case 7:
                return "创新型封闭式";
            case 8:
                return "创新型开放式";
            default:
                return "未知";
        }
    }
    /// <summary>
    /// 从运河获取基金类型
    /// </summary>
    /// <param name="typeid"></param>
    /// <returns></returns>
    public string getFundTypeFromCanal(int typeid)
    {
        switch (typeid)
        {
            case 0:
                return "其它";
            case 1:
                return "契约型封闭式";
            case 2:
                return "普通契约型开放式";
            case 3:
                return "ETF";
            case 4:
                return "LOF";
            case 5:
                return "LOF";
            default:
                return "契约型封闭式";

        }
    }
    /// <summary>
    /// 获取基金投资类型
    /// </summary>
    /// <param name="typeid"></param>
    /// <returns></returns>
    public string getInvestType(int typeid)
    {
        switch (typeid)
        {
            case 0:
                return "股票型";
            case 1:
                return "债券型";
            case 2:
                return "混合型";
            case 3:
                return "货币型";
            case 4:
                return "ETF联接基金";
            default:
                return "未知";
        }
    }

    /// <summary>
    /// 从运河获取基金投资类型
    /// </summary>
    /// <param name="typeid"></param>
    /// <returns></returns>
    public string getInvestTypeFromCanal(int typeid)
    {
        switch (typeid)
        {
            case 0:
                return "其它";
            case 1:
                return "股票型";
            case 2:
                return "债券型";
            case 3:
                return "混合型";
            case 4:
                return "货币型";
            default:
                return "ETF联接基金";
        }
    }

    /// <summary>
    /// 获取基金申购、赎回状态
    /// </summary>
    /// <param name="satus"></param>
    /// <returns></returns>
    public string getPurchaseStatus(int typeid)
    {
        switch (typeid)
        { 
            case 1:
                return "开放";
            case 2:
                return "暂停";
            default:
                return "未知";
        }
    }
    /// <summary>
    /// 将运河中的基金类型转换为SG接口中的证券类型
    /// </summary>
    /// <param name="typeid"></param>
    /// <returns></returns>
    public string getSGSercurityTypebyFundType(int typeid)
    {
        switch (typeid)
        {
            case 0:
                return "500";
            case 1:
                return "501";
            case 2:
                return "502";
            case 3:
                return "503";
            case 4:
                return "504";
            default:
                return "501";
        }
    }

    /// <summary>
    /// 获取基金持有类型
    /// </summary>
    /// <param name="typeid"></param>
    /// <returns></returns>
    public string getHoldType(int typeid) 
    {
        switch (typeid)
        {
            case 1:
                return "新增";
            case 2:
                return "剔除";
            case 3:
                return "增仓";
            case 4:
                return "减仓";
            default:
                return "未知";
        }
    }

    /// <summary>
    /// 获取时间列表
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public string getDateSelect(string dt) 
    {
        int year = DateTime.Now.Year;
        DateTime startDt = DateTime.Now;
        StringBuilder result = new StringBuilder();
        if (DateTime.Now.Month >= 4 && DateTime.Now.Month < 7)
        {
            startDt = new DateTime(year, 3, 31);
        }
        else if (DateTime.Now.Month >= 7 && DateTime.Now.Month < 10)
        {
            startDt = new DateTime(year, 6, 30);
        }
        else if (DateTime.Now.Month >= 10 && DateTime.Now.Month <= 12)
        {
            startDt = new DateTime(year, 9, 30);
        }
        else 
        {
            startDt = new DateTime(year - 1, 12, 31);
        }
        result.AppendLine("<option value=\"\">全部</option>");
        if (startDt.ToString("yyyy-MM-dd") == dt)
        {
            result.AppendLine("<option selected=\"selected\" value=\"" + startDt.ToString("yyyy-MM-dd") + "\">" + startDt.ToString("yyyy-MM-dd") + "</option>");
        }
        else 
        {
            result.AppendLine("<option value=\"" + startDt.ToString("yyyy-MM-dd") + "\">" + startDt.ToString("yyyy-MM-dd") + "</option>");
        }
        for (int i = 1; i <= 10; i++) 
        {
            DateTime tmpDate = startDt.AddMonths(i * 3 * -1);
            if (tmpDate.Month == 3 || tmpDate.Month == 12) 
            {
                tmpDate = new DateTime(tmpDate.Year, tmpDate.Month, 31);
            }
            if (tmpDate.ToString("yyyy-MM-dd") == dt)
            {
                result.AppendLine("<option selected=\"selected\" value=\"" + tmpDate.ToString("yyyy-MM-dd") + "\">" + tmpDate.ToString("yyyy-MM-dd") + "</option>");
            }
            else
            {
                result.AppendLine("<option value=\"" + tmpDate.ToString("yyyy-MM-dd") + "\">" + tmpDate.ToString("yyyy-MM-dd") + "</option>");
            }
        }
        return result.ToString();
    }

    /// <summary>
    /// 获取封闭式基金最新价
    /// </summary>
    /// <param name="code"></param>
    /// <param name="mk"></param>
    /// <returns></returns>
    public float getQuotePrice(string code, byte mk) 
    {
        StockCode stock = new StockCode();
        stock.code = code;
        stock.marketType = mk;
        StockSnap snap = quoteOper.GetStockSnap(stock);
        float re = snap.Close;
        if (snap.UpDownPer < 0) 
        {
            re = re * -1;
        }
        return re;
    }

    /// <summary>
    /// 记录最近访问基金
    /// </summary>
    /// <param name="fundcode"></param>
    /// <param name="fundname"></param>
    /// <param name="mk"></param>
    public void writeCookies(string fundcode, string fundname, string mk)
    {
        HttpRequest Request = HttpContext.Current.Request;
        HttpResponse Response = HttpContext.Current.Response;
        string codeList = "";
        string fundmsg = fundcode + "|" + fundname + "|" + mk;
        if (Request.Cookies["FundVisistnew"] != null && Request.Cookies["FundVisistnew"].Value != "")
        {
            codeList = HttpContext.Current.Server.UrlDecode(Request.Cookies["FundVisistnew"].Value);
            int count = codeList.IndexOf(fundmsg);
            if (count > 0)
            {
                codeList = codeList.Replace("__" + fundmsg, "");
            }
            if (count == 0)
            {
                codeList = codeList.Replace(fundmsg + "__", "");
                codeList = codeList.Replace(fundmsg, "");
            }
            string[] codes = Regex.Split(codeList, "__");
            /**2010-08-25修改**/
            //if (codes.Length >= 6)
            if (codes.Length >= 8)
            /**2010-08-25修改 end **/
            {
                codeList = fundmsg + "__" + codeList.Replace("__" + codes[4], "");
            }
            else
            {
                if (codeList != "")
                {
                    codeList = fundmsg + "__" + codeList;
                }
                else
                {
                    codeList = fundmsg;
                }
            }
        }
        else
        {
            codeList = codeList + fundmsg;
        }
        Response.Cookies["FundVisistnew"].Expires = DateTime.Now.AddMonths(1);
        Response.Cookies["FundVisistnew"].Path = "/";
        Response.Cookies["FundVisistnew"].Domain = "stockstar.com";
        Response.Cookies["FundVisistnew"].Value = HttpContext.Current.Server.UrlEncode(codeList);
    }

    /// <summary>
    /// 获取首页中基金相关链接
    /// </summary>
    /// <param name="fundcode"></param>
    /// <returns></returns>
    public string getIndexLink(string fundcode)
    {
        StringBuilder result = new StringBuilder();
        result.AppendLine("<table width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        result.AppendLine("<tr style=\"line-height:22px;\">");
        result.AppendLine("<td><a href=\"" + PubBean.getFundZS(fundcode, 0) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_16.gif\" alt=\"净值走势图\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\""+PubBean.getFundZS(fundcode,0)+"\">走势</a></td>");
        result.AppendLine("<td><a href=\"" + PubBean.getFundJZ(fundcode, 0) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_17.gif\" alt=\"基金净值\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getFundJZ(fundcode, 0) + "\">净值</a></td>");
        result.AppendLine("<td><a href=\"" + PubBean.getBarUrl(fundcode) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_18.gif\" alt=\"基金吧\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getBarUrl(fundcode) + "\">吧</a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"tab/fundfav.aspx?fundcode=" + fundcode + "\"><img src=\"http://i.ssimg.cn/images/fund/pufundjia.gif\" alt=\"加入自选基金\" border=\"0\" /> 自选基金</a></td>");
        result.AppendLine("</tr>");
        result.AppendLine("</table>");
        return result.ToString();
    }

    /// <summary>
    /// 获取首页中基金相关链接
    /// </summary>
    /// <param name="fundcode"></param>
    /// <returns></returns>
    public string getIndexLink(string fundcode,string pid)
    {
        StringBuilder result = new StringBuilder();
        result.AppendLine("<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        result.AppendLine("<tr style=\"line-height:22px;\">");
        result.AppendLine("<td><a id=\"" + pid + "_p1\" href=\"" + PubBean.getFundZS(fundcode, 0) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_16.gif\" alt=\"净值走势图\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_a1\" class=\"link_blank\" href=\"" + PubBean.getFundZS(fundcode, 0) + "\">走势</a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_p2\" href=\"" + PubBean.getFundJZ(fundcode, 0) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_17.gif\" alt=\"基金净值\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_a2\" class=\"link_blank\" href=\"" + PubBean.getFundJZ(fundcode, 0) + "\">净值</a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_p3\" href=\"" + PubBean.getBarUrl(fundcode) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_18.gif\" alt=\"基金吧\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_a3\" class=\"link_blank\" href=\"" + PubBean.getBarUrl(fundcode) + "\">吧</a></td>");
        
        result.AppendLine("<td><a id=\"" + pid + "_a4\" class=\"link_blank\" href=\"/fav/tab/fundfav.aspx?fundcode=" + fundcode + "\"><img src=\"http://i.ssimg.cn/images/fund/pufundjia.gif\" alt=\"加入自选基金\" border=\"0\" /> 自选基金</a></td>");
        result.AppendLine("</tr>");
        result.AppendLine("</table>");
        return result.ToString();
    }

    /// <summary>
    /// 获取首页中自选基金相关链接
    /// </summary>
    /// <param name="fundcode"></param>
    /// <returns></returns>
    public string getIndexLinkFav(string fundcode, string pid)
    {
        StringBuilder result = new StringBuilder();
        result.AppendLine("<table width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        result.AppendLine("<tr style=\"line-height:22px;\">");
        result.AppendLine("<td><a id=\"" + pid + "_p1\" href=\"" + PubBean.getFundZS(fundcode, 0) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_16.gif\" alt=\"净值走势图\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_a1\" class=\"link_blank\" href=\"" + PubBean.getFundZS(fundcode, 0) + "\">走势图</a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_p2\" href=\"" + PubBean.getFundJZ(fundcode, 0) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_17.gif\" alt=\"基金净值\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_a2\" class=\"link_blank\" href=\"" + PubBean.getFundJZ(fundcode, 0) + "\">基金净值</a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_p3\" href=\"" + PubBean.getBarUrl(fundcode) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_18.gif\" alt=\"基金吧\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_a3\" class=\"link_blank\" href=\"" + PubBean.getBarUrl(fundcode) + "\">基金吧</a></td>");
        result.AppendLine("</tr>");
        result.AppendLine("</table>");
        return result.ToString();
    }

    /// <summary>
    /// 获取表格中基金相关链接
    /// </summary>
    /// <param name="fundcode"></param>
    /// <returns></returns>
    public string getTabLink(string fundcode)
    {
        StringBuilder result = new StringBuilder();
        result.AppendLine("<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        result.AppendLine("<tr style=\"line-height:22px;\">");
        result.AppendLine("<td><a href=\"" + PubBean.getFundZS(fundcode, 2) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_16.gif\" alt=\"净值走势图\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getFundZS(fundcode, 2) + "\">走势</a></td>");
        result.AppendLine("<td><a href=\"" + PubBean.getFundJZ(fundcode, 2) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_17.gif\" alt=\"基金净值\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getFundJZ(fundcode, 2) + "\">净值</a></td>");
        result.AppendLine("<td><a href=\"" + PubBean.getBarUrl(fundcode) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_18.gif\" alt=\"基金吧\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getBarUrl(fundcode) + "\">吧</a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"/fav/tab/fundfav.aspx?fundcode=" + fundcode + "\"><img src=\"http://i.ssimg.cn/images/fund/pufundjia.gif\" alt=\"加入自选基金\" border=\"0\" /> 自选基金</a></td>");
        result.AppendLine("</tr>");
        result.AppendLine("</table>");
        return result.ToString();
    }

    /// <summary>
    /// 获取F10相关页面的行情链接
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public string getF10QuoteUrl(string code)
    {
        StringBuilder result = new StringBuilder();
        result.AppendLine("<table width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        result.AppendLine("<tr style=\"line-height:22px;\">");
        result.AppendLine("<td><a href=\"" + PubBean.getQuoteUrl(code) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_11.gif\" alt=\"行情\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getQuoteUrl(code) + "\">行情</a></td>");
        result.AppendLine("<td><a href=\"" + PubBean.getQuoteBar(code) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_13.gif\" alt=\"股吧\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getNewsUrl(code) + "\">资讯</a></td>");
        result.AppendLine("<td><a href=\"" + PubBean.getNewsUrl(code) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_15.gif\" alt=\"资讯\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getQuoteBar(code) + "\">股吧</a></td>");
        result.AppendLine("</tr>");
        result.AppendLine("</table>");
        return result.ToString();
    }

    /// <summary>
    /// 获取首页相关页面的行情链接
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public string getIndexQuoteUrl(string code)
    {
        StringBuilder result = new StringBuilder();
        result.AppendLine("<table width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        result.AppendLine("<tr style=\"line-height:22px;\">");
        result.AppendLine("<td><a href=\"" + PubBean.getQuoteUrl(code) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_11.gif\" alt=\"行情\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getQuoteUrl(code) + "\">行情</a></td>");
        result.AppendLine("<td><a href=\"" + PubBean.getQuoteBar(code) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_13.gif\" alt=\"股吧\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getNewsUrl(code) + "\">资讯</a></td>");
        result.AppendLine("<td><a href=\"" + PubBean.getNewsUrl(code) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_15.gif\" alt=\"资讯\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a class=\"link_blank\" href=\"" + PubBean.getQuoteBar(code) + "\">股吧</a></td>");
        result.AppendLine("</tr>");
        result.AppendLine("</table>");
        return result.ToString();
    }

    /// <summary>
    /// 获取首页相关页面的行情链接
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public string getIndexQuoteUrl(string code,string pid)
    {
        StringBuilder result = new StringBuilder();
        result.AppendLine("<table width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        result.AppendLine("<tr style=\"line-height:22px;\">");
        result.AppendLine("<td><a id=\"" + pid + "_p1\" href=\"" + PubBean.getQuoteUrl(code) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_11.gif\" alt=\"行情\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_a1\" class=\"link_blank\" href=\"" + PubBean.getQuoteUrl(code) + "\">行情</a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_p2\" href=\"" + PubBean.getQuoteBar(code) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_13.gif\" alt=\"股吧\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_a2\" class=\"link_blank\" href=\"" + PubBean.getNewsUrl(code) + "\">资讯</a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_p3\" href=\"" + PubBean.getNewsUrl(code) + "\"><img src=\"http://i.ssimg.cn/images/fund/icon_15.gif\" alt=\"资讯\" width=\"16\" height=\"15\" border=\"0\" /></a></td>");
        result.AppendLine("<td><a id=\"" + pid + "_a3\" class=\"link_blank\" href=\"" + PubBean.getQuoteBar(code) + "\">股吧</a></td>");
        result.AppendLine("</tr>");
        result.AppendLine("</table>");
        return result.ToString();
    }

    public string showTabPages(int pageCount, int pageId, string more)
    {
        StringBuilder result = new StringBuilder();
        string urlpath = HttpContext.Current.Request.Path;
        //HttpContext.Current.Response.Write(urlpath);
        if ((pageId > pageCount) && pageCount > 0)
        {
            pageId = pageCount;
        }
        if (pageCount > 1)
        {            
            result.Append("<script type=\"text/javascript\">");
            result.Append("function pchk(pageid){");
            result.Append("window.location = \"" + urlpath + "?pageid=\"+pageid+\"" + more + "\";");
            result.Append("}</script>");

            result.Append("<div class=\"fanyi_all\">");
            result.Append("第" + pageId + "/" + pageCount + "页");
            result.Append("&nbsp;<a class=\"link_blank\" href=\"" + urlpath + "?pageid=1" + more + "\">首页</a>");
            if (pageId > 1)
            {
                result.Append("&nbsp;<a class=\"link_blank\" href=\"" + urlpath + "?pageid=" + (pageId - 1) + more + "\">上页</a>");
            }
            if (pageId < 5)
            {
                for (int i = 1; i <= 5; i++)
                {
                    if (i != pageId)
                    {
                        result.Append("&nbsp;<a class=\"link_blank\" href=\"" + urlpath + "?pageid=" + i + more + "\">" + i + "</a>");
                    }
                    else 
                    {
                        result.Append("&nbsp;<strong>" + i + "</strong>");
                    }
                }
            }
            else 
            {
                for (int i = (pageId - 2); i <= (pageId + 2) && i <= pageCount; i++)
                {
                    if (i != pageId)
                    {
                        result.Append("&nbsp;<a class=\"link_blank\" href=\"" + urlpath + "?pageid=" + i + more + "\">" + i + "</a>");
                    }
                    else
                    {
                        result.Append("&nbsp;<strong>" + i + "</strong>");
                    }
                }
            }
            if (pageId < pageCount)
            {
                result.Append("&nbsp;<a class=\"link_blank\" href=\"" + urlpath + "?pageid=" + (pageId + 1) + more + "\">下页</a>");
            }
            result.Append("&nbsp;<a class=\"link_blank\" href=\"" + urlpath + "?pageid=" + pageCount + more + "\">末页</a>");
            result.Append("&nbsp;到&nbsp;<select name=\"\" onchange=\"pchk(this.value)\">");
            for (int i = 1; i <= pageCount; i++)
            {
                if (i == pageId)
                {
                    result.Append("<option value=\"" + i + "\" selected=\"selected\">" + i + "</option>");
                }
                else 
                {
                    result.Append("<option value=\"" + i + "\">" + i + "</option>");
                }
            }
            result.Append("</select>&nbsp;页");
            result.Append("</div>");

        }
        else if (pageCount == -1) 
        {
            result.Append("<div class=\"fanyi_all\">");
            if (pageId > 1)
            {
                result.Append("&nbsp;<a class=\"link_blank\" href=\"" + urlpath + "?pageid=" + (pageId - 1) + more + "\">&lt;&lt;上页</a>");
            }
            result.Append("&nbsp;<a class=\"link_blank\" href=\"" + urlpath + "?pageid=" + (pageId + 1) + more + "\">下页&gt;&gt;</a>");
            result.Append("</div>");
        }
        return result.ToString();
    }

    public string getAllTopTree(int index) 
    {
        StringBuilder result=new StringBuilder();
        if (index == 1) 
        {
            result.Append("<b><a href=\"fundgsjj.aspx\">[基金公司]</a></b>&nbsp;");
        }
        else
        {
            result.Append("<a class=\"link_hui\" href=\"fundgsjj.aspx\">[基金公司]</a>&nbsp;");
        }
        if (index == 2)
        {
            result.Append("<b><a href=\"fundallcode.aspx\">[基金代码查询]</a></b>&nbsp;");
        }
        else
        {
            result.Append("<a class=\"link_hui\" href=\"fundallcode.aspx\">[基金代码查询]</a>&nbsp;");
        }
        if (index == 3)
        {
            result.Append("<b><a href=\"fundalljz.aspx\">[基金净值比较]</a></b>&nbsp;");
        }
        else
        {
            result.Append("<a class=\"link_hui\" href=\"fundalljz.aspx\">[基金净值比较]</a>&nbsp;");
        }
        if (index == 4)
        {
            result.Append("<b><a href=\"fundzcg.aspx\">[基金重仓股]</a></b>&nbsp;");
        }
        else
        {
            result.Append("<a class=\"link_hui\" href=\"fundzcg.aspx\">[基金重仓股]</a>&nbsp;");
        }
        if (index == 5)
        {
            result.Append("<b><a href=\"fundallfh.aspx\">[基金分红比较]</a></b>&nbsp;");
        }
        else
        {
            result.Append("<a class=\"link_hui\" href=\"fundallfh.aspx\">[基金分红比较]</a>&nbsp;");
        }
        if (index == 6)
        {
            result.Append("<b><a href=\"fundallgm.aspx\">[基金规模比较]</a></b>&nbsp;");
        }
        else
        {
            result.Append("<a class=\"link_hui\" href=\"fundallgm.aspx\">[基金规模比较]</a>&nbsp;");
        }
        if (index == 7)
        {
            result.Append("<b><a href=\"fundallzcpz.aspx\">[基金资产配置比较]</a></b>&nbsp;");
        }
        else
        {
            result.Append("<a class=\"link_hui\" href=\"fundallzcpz.aspx\">[基金资产配置比较]</a>&nbsp;");
        }
        if (index == 8)
        {
            result.Append("<b><a href=\"fundallhytz.aspx\">[基金行业投资比较]</a></b>&nbsp;");
        }
        else
        {
            result.Append("<a class=\"link_hui\" href=\"fundallhytz.aspx\">[基金行业投资比较]</a>&nbsp;");
        }
        return result.ToString();
    }

    public string getOptionList(List<KeyValue> list,string key) 
    {
        StringBuilder result = new StringBuilder();
        foreach (KeyValue tmpValue in list) 
        {
            DateTime tmpDate = DateTime.Now;
            if (tmpValue.KeyF == key)
            {                
                if (!DateTime.TryParse(tmpValue.KeyF, out tmpDate))
                {
                    result.Append("<option value=\"" + tmpValue.KeyF + "\" selected=\"selected\">" + tmpValue.ValueF + "</option>");
                }
                else 
                {
                    result.Append("<option value=\"" + tmpDate.ToString("yyyy-MM-dd") + "\" selected=\"selected\">" + tmpDate.ToString("yyyy-MM-dd") + "</option>");
                }
            }
            else 
            {
                if (!DateTime.TryParse(tmpValue.KeyF, out tmpDate))
                {
                    result.Append("<option value=\"" + tmpValue.KeyF + "\">" + tmpValue.ValueF + "</option>");
                }
                else 
                {
                    result.Append("<option value=\"" + tmpDate.ToString("yyyy-MM-dd") + "\">" + tmpDate.ToString("yyyy-MM-dd") + "</option>");
                }
            }
        }
        return result.ToString();
    }

    public string getFundComList()
    {
        BaseInfoFundCom.FundCom fundoper = new BaseInfoFundCom.FundCom();
        ParamKeyValue param = new ParamKeyValue();
        param.keyfield = "glcode";
        param.valuefield = "glname";
        param.sortfields = " valueF ";
        param.investtype = -1;
        return getOptionList(fundoper.GetKeyValueList(param, 16), "");
    }

    /// <summary>
    /// 输出评级图片
    /// </summary>
    /// <param name="score"></param>
    /// <returns></returns>
    public string displayPJImage(double score)
    {
        string imgsrc = null;
        double maxvalue = Math.Floor(score);
        for (int i = 0; i < maxvalue; i++)
            imgsrc += "<img src=\"http://i.ssimg.cn/images/fund/star_22.gif\">";
        if (maxvalue != score)
            imgsrc += "<img src=\"http://i.ssimg.cn/images/fund/star_23.gif\">";

        return imgsrc;
    }

    /// <summary>
    /// 获取评级图片
    /// </summary>
    /// <param name="fundcode"></param>
    /// <param name="fundoper"></param>
    /// <returns></returns>
    public string getFundPj(string fundcode, BaseInfoFundCom.FundCom fundoper) 
    {
        ParamPJ parampj = new ParamPJ();
        parampj.offset = 1;
        parampj.sortfields = "ZHPJ desc";
        parampj.investtype = -1;
        parampj.fundcode = fundcode;
        List<FundPJ> fundpj = fundoper.GetFundPJList(parampj);
        if (fundpj.Count > 0) 
        {
            try
            {
                return displayPJImage(double.Parse(fundpj[0].ZHPJ.ToString()));
            }
            catch 
            {
                return "";
            }
        }
        return "";
    }

    /// <summary>
    /// 总表页中排序
    /// </summary>
    /// <param name="title"></param>
    /// <param name="sCol"></param>
    /// <param name="col"></param>
    /// <param name="sortType"></param>
    /// <param name="more"></param>
    /// <returns></returns>
    public string getHeadSortStr(string title, int sCol, int col, int sortType,string more) 
    {
        string urlpath = HttpContext.Current.Request.Path;
        
        string sortArr = "▲";
        if (sortType == 2)
        {
            sortArr = "▲";
        }
        else 
        {
            sortArr = "▼";
        }
        if (sCol == col)
        {
            if (sortType == 1)
            {
                sortType = 2;
            }
            else 
            {
                sortType = 1;
            }
            return ("<a href=\"" + urlpath + "?col=" + col + "&flag=" + sortType + more + "\" target=\"_self\">" + title + sortArr + "</a>");
        }
        else 
        {
            return ("<a href=\"" + urlpath + "?col=" + col + "&flag=1" + more + "\" target=\"_self\">" + title + "</a>");
        }
    }

    public string getHqTime(uint time) 
    {
        string result = "";
        string hh = (time / 10000).ToString("00");
        string mm = ((time % 10000) / 100).ToString("00");
        string ss = (time % 100).ToString("00");
        return hh + ":" + mm + ":" + ss;
    }

    public string chkNullArg(Object arg) 
    {
        try
        {
            return arg.ToString();
        }
        catch
        {
            return "";
        }
    }

    public FundCode getNewFundCode(string code)
    {
        FundCode rtn = null;
        try
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://192.168.12.208:8088/f10/V_BS_FUND_RELEASE/limit=1&full=1&filter-FUND_CODE-str=" + code);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
            System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding);
            string htmlstr = reader.ReadToEnd();

            htmlstr = htmlstr.Replace(htmlstr.Split('{')[0], "").Replace(".", "").Replace(";", "");
            reader.Close();
            response.Close();


            JavaScriptSerializer s = new JavaScriptSerializer();
            Dictionary<string, object> JsonData = (Dictionary<string, object>)s.DeserializeObject(htmlstr);
            object[] rows = (object[])JsonData["rows"];
            foreach (object ob in rows)
            {
                rtn = new FundCode();
                Dictionary<string, object> val = (Dictionary<string, object>)ob;
                rtn.Code = val["FUND_CODE"].ToString();
                rtn.SimpleName = val["MANAS_NAME"].ToString();
                rtn.InvestType = Convert.ToInt32(val["INVST_TYPE"]);
                rtn.SecurityType = Convert.ToInt32(val["FND_TYPE"]);
                break;
            }
        }
        catch { }
        return rtn;
    }
}
